<?php

namespace Api\Controller;

use Think\Controller;

// 跨域
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:x-requested-with, content-type,token');
class UserController extends Controller
{
    // 修改密码
    function edit_user_password()
    {
        $oldPassword = $_POST['oldPassword'];
        $newPassword = $_POST['newPassword'];
        $username = $_POST['username'];
        if ($oldPassword == null or $newPassword == null or $username == null) $this->ajaxReturn(['state' => 0, 'info' => '请检查数据是否输入正确']);

        $where['password'] = $oldPassword;
        $where['username'] = $username;
        $user = M('user')->where($where)->find();
        if (!$user) $this->ajaxReturn(['state' => 0, 'info' => '请检查旧密码是否正确']);

        $data['password'] = $newPassword;
        M('user')->where('username = "' . $username . '"')->save($data);
        $this->ajaxReturn(['state' => 1, 'info' => '修改成功']);
    }

    // 搜索用户
    function get_user()
    {
        // 分页
        $page = $_POST['page'];
        $current = $page['current'] ? $page['current'] : 1;
        $size = $page['size'] ? $page['size'] : 10;
        // 条件
        $query = $_POST['query'];

        // 单个
        if ($query['id']) $where['id'] = $query['id'];
        // 用户名
        if ($query['username']) $where['username'] = ['like', '%' . $query['username'] . '%'];
        //删除管理员
        $where[0]['username'] = ['NEQ', 'admin'];

        $count = M('user')->where($where)->count();
        $data = M('user')->field('id,username,router,logo,addtime')->where($where)->order('addtime desc')->limit(($size * $current) - $size, $size)->select();

        $this->ajaxReturn(['state' => 1, 'info' => '搜索成功', 'data' => $data, 'count' => intval($count)]);
    }

    // 获取登录用户信息
    function get_login_user()
    {
        $token = $_SERVER['HTTP_TOKEN'];
        $username = M('login')->where('token="' . $token . '"')->find()['username'];
        $user = M('user')->where('username="' . $username . '"')->find();
        unset($user['password']);
        $this->ajaxReturn(['state' => 1, 'info' => '查询成功', 'data' => $user]);
    }

    // 删除用户
    function del_user()
    {
        $id = $_POST['id'];
        if ($id == null) $this->ajaxReturn(['state' => 0, 'info' => '删除id不能为空']);
        M('user')->delete(implode(',', $id));
        $this->ajaxReturn(['state' => 1, 'info' => '删除成功']);
    }

    // 添加修改用户信息
    function handle_user()
    {
        $username = $_POST['username'];
        if ($username == null) $this->ajaxReturn(['state' => 0, 'info' => '请检查数据是否输入正确']);
        if ($_POST['id']) {
            $info = '修改成功';
            M('user')->save($_POST);
        } else {
            $info = '添加成功';
            $_POST['addtime'] =  date("Y-m-d H:i:s");
            M('user')->add($_POST);
        }
        $this->ajaxReturn(['state' => 1, 'info' => $info]);
    }


    // 所有函数入口
    function _initialize()
    {
        // 白名单
        $whitelist = ['get_login_user'];
        $url = explode('/', $_SERVER['PATH_INFO']);
        $method = $url[count($url) - 1];
        // 需要验证token
        if (!in_array($method, $whitelist)) {
            $token = $_SERVER['HTTP_TOKEN'];
            $where['token'] = $token;
            $where['expire_time'] = ['gt', date("Y-m-d H:i:s")];
            $login = M('login')->where($where)->find();
            if (!$login) $this->ajaxReturn(['state' => 0, 'info' => '登录已失效 , 请重新登录']);
        }
    }
}
